(function($, doc) {
	$.init();
	var userinfo = app.getState() || {};
	doc.getElementById('nickname').value=userinfo.nickname;
	
	$.plusReady(function() {
		doc.getElementById("set_btn").addEventListener('tap', function() {
			var Info={
				user_id: userinfo.user_id,
				nickname: doc.getElementById('nickname').value,
				_token:userinfo.token
			};
			if(Info.nickname.trim()==''){
				plus.nativeUI.toast('请输入昵称');
				return;
			}
		    if(Info.nickname.trim()==userinfo.nickname){
				plus.nativeUI.toast('新昵称与旧昵称一样');
				return;
			}
		    mui.ajax({
				url: apiurl + "User/set_nickname/",
				data: Info,
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'post',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					if(res.status ==-3){
						mui.toast(res.msg);
						app.out();
						return false;
					}
					
					// 请求成功
					if(res.status ==0) {
						mui.toast(res.msg);
					} else {
						mui.toast('更换昵称成功');
						userinfo.nickname=Info.nickname.trim();
						app.setState(userinfo);
						
						var settingPage = plus.webview.getWebviewById('setting.html');
                        mui.fire(settingPage,'refresh_nickname');
                        mui.fire(plus.webview.getWebviewById('index.html'),'refresh_nickname');
                        mui.back();
						
					}
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('操作失败！');
				}
			});
		});
		
	})
	
}(mui, document));